package com.sk.cardeal.act.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.Space;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.act.MainActivity;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.UserBean;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MD5;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.utils.SPUtils;
import com.sk.cardeal.utils.SnackUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.sk.cardeal.utils.MyUtils.setTranslucent;

/**
 * Created by sk on 2018/2/2.
 */

public class LoginActivity extends BaseActivity implements HandleDataCallBack {

    @BindView(R.id.ed_account)
    EditText edAccount;
    @BindView(R.id.ed_password)
    EditText edPassword;
    @BindView(R.id.login)
    TextView login;

    private LoadingDialog loadingDialog;

    @Override
    protected int getContentView() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTranslucent(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        if (SPUtils.contains(mContext, "userInfo")) {
            Intent intent = new Intent(mContext, MainActivity.class);
            startActivity(intent);
            finish();
        }
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
    }


    @OnClick(R.id.login)
    public void onViewClicked() {

        if (TextUtils.isEmpty(edAccount.getText().toString())) {
            SnackUtils.showError(mRootView, "请输入您的登录账号！");
            return;
        }
        if (TextUtils.isEmpty(edPassword.getText().toString())) {
            SnackUtils.showError(mRootView, "请输入您的登录密码！");
            return;
        }


        loadingDialog.show();
        AppApi.loginAccount(edAccount.getText().toString(), MD5.md5(edPassword.getText().toString()), LoginActivity.this, 0, LoginActivity.this);
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        SPUtils.put(mContext, "userInfo", result);
        Intent intent = new Intent(mContext, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

}
